package com.uit3j.cloud.auth.controller;

import cn.hutool.core.collection.ListUtil;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.uit3j.cloud.auth.application.CounterSynchronized;
import com.uit3j.cloud.auth.application.TicketSource;
import com.uit3j.cloud.auth.vo.People;
import com.uit3j.cloud.common.response.ServerResponseEntity;
import com.uit3j.cloud.common.util.Json;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tan
 * @date 2020/7/30
 */
@RestController
@RequestMapping("/ua/captcha")
@Tag(name = "验证码")
public class CaptchaController {

	private final CaptchaService captchaService;

	public CaptchaController(CaptchaService captchaService) {
		this.captchaService = captchaService;
	}

	@PostMapping({ "/get" })
	public ServerResponseEntity<ResponseModel> get(@RequestBody CaptchaVO captchaVO) {

		List<People> peopleList = ListUtil.toList(
			new People(1, "张三", "西安", 22),
			new People(2, "李四", "咸阳", 23),
			new People(3, "王五", "榆林", 24),
			new People(4, "赵六", "宝鸡", 25),
			new People(5, "孙七", "延安", 26)
		);
		Map<Integer,String> collect = peopleList.stream().collect(Collectors.toMap(People::getId,People::getName));
		System.out.println("collect = " + collect);
		String jsonStrCollect = Json.toJsonString(collect);
		System.out.println("jsonStringMap =" + jsonStrCollect);

		Map<Integer,People> collect1 = peopleList.stream().collect(Collectors.toMap(People::getId,o->o));
		System.out.println("collect1 = " + collect1);
		String jsonStrMap = Json.toJsonString(collect1);
		System.out.println("jsonStringMap = " + jsonStrMap);


		List<People> collect3 = peopleList.stream().collect(Collectors.toList());
		System.out.println("collect3 = " + collect3);

		String jsonStr = Json.toJsonString(collect3);
		System.out.println("jsonString =" + jsonStr);

		// 多线程
//		MyThread st = new MyThread();
//		st.useThread();

		threadWhile(10);

		CounterSynchronized cs = new CounterSynchronized();
		cs.mainMethod();

//		return ServerResponseEntity.success();
		return ServerResponseEntity.success(captchaService.get(captchaVO));
	}

	@PostMapping({ "/check" })
	public ServerResponseEntity<ResponseModel> check(@RequestBody CaptchaVO captchaVO) {
		return ServerResponseEntity.success(captchaService.check(captchaVO));
	}

	public String apply(People people) {
		people.setName("姓名：" + people.getName());
		return people.getName();
	}

	public People addAge(People people){
		people.setAge(people.getAge()+1);
		return people;
	}

	/**
	 * 调用线程数量，
	 * @param threadNum
	 */
	private static void threadWhile(int threadNum){
		for(int i = 1; i <= threadNum; i++){
			new Thread(() -> {
				while (true) {
					int currentTickets = TicketSource.saleTickets();
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if (currentTickets <= 0) break;
				}
			}, "线程0"+i).start();
		}
	}



}
